import { LOCAL_KEY } from '@/constant/dict';
import { mapActions } from 'vuex';

export default {
    data() {
        return {
            intervalId: undefined, // 轮询计时器ID
            pollInterval: 10 * 1000 // 轮询间隔时间
        };
    },
    methods: {
        ...mapActions('config', ['LOAD_CONFIG']),
        startPolling() {
            if (localStorage.getItem(LOCAL_KEY.IS_AUTH_TOKEN)) {
                if (this.$route.path != '/login') {
                    if (!this.intervalId) {
                        this.intervalId = setInterval(async () => {
                            if (this.$store.state.globalTimerStopFlag) {
                                clearInterval(this.intervalId);
                            } else {
                                this.LOAD_CONFIG();
                            }
                        }, this.pollInterval);
                    }
                }
            } else {
                this.stopPolling();
            }
        },
        stopPolling() {
            clearInterval(this.intervalId);
        }
    }
};
